package geekTime.chapter18StampedLock;

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.StampedLock;

/**
 * 使用StampedLock时，一定不要使用中断操作，不然会使CPU使用量飙升100%，程序卡死
 * Created by LinGer on 2019/7/1
 */
public class DoNotUseInterrupt extends Thread{

    public static void main(String[] args) throws InterruptedException {
        final StampedLock lock = new StampedLock();

        Thread t1 = new DoNotUseInterrupt(){
            @Override
            public void run() {
                //获取写锁
                lock.writeLock();
                //永远阻塞在此处，不释放写锁
                LockSupport.park();
            }
        };
        t1.start();
        //保证t1获取到写锁
        Thread.sleep(1000);

        //t2
        Thread t2 = new DoNotUseInterrupt(){
            @Override
            public void run() {
                //阻塞在悲观读锁
                lock.readLock();
            }
        };
        t2.start();
        //保证t2阻塞在读锁
        Thread.sleep(1000);

        //中断线程t2,会导致t2所在CPU飙升
        t2.interrupt();
        t2.join();
    }
}
